package de.ama.search;

import de.ama.crawler.Entry;
import de.ama.crawler.Store;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by IntelliJ IDEA.
 * User: x
 * Date: 20.02.11
 * Time: 00:39
 * To change this template use File | Settings | File Templates.
 */
public class WordIndex {

    private static WordIndex instance = null;

    public static WordIndex get(){
        if(instance==null){
            instance = new WordIndex();
        }
        return instance;
    }

    String[] ignores = new String[]{};

    Map<Character,TreeMap<String,Word>> cache = new TreeMap<Character, TreeMap<String,Word>>();

    public void addWord(String txt, int id){
        if(prepare(txt)==null) return;
        txt = txt.toLowerCase();
        char key = txt.toLowerCase().charAt(0);

        TreeMap<String,Word> words = cache.get(key);
        if(words==null){
            words = new TreeMap<String,Word>();
            cache.put(key,words);
        }

        Word word = words.get(txt);

        if(word==null){
           word = new Word(txt);
           words.put(txt,word);
        }

        word.addId(id);
    }

    private String prepare(String txt) {
        if(txt == null) return null;
        txt = txt.trim();
        if(txt.length()==0) return null;
        for (int i = 0; i < ignores.length; i++) {
            String ignore = ignores[i];
            if(txt.equals(ignore)) return null;
        }
        return txt;
    }

    public List<Integer> findIds(String txt){
        List<Integer> ret = new ArrayList<Integer>();
        if(txt.trim().indexOf(" ")>0){
            String[] words = txt.split(" ");
            for (String word : words) {
                List<Integer> idsForWord = findIdsForWord(word);
                for (Integer id : idsForWord) {
                    Entry entry = Store.get().findMovieById(id);
                    if(entry!=null && entry.containsString(txt)){
                        ret.add(entry.getId());
                    }
                }
            }
        } else {
            ret = findIdsForWord(txt);
        }
        return ret;
    }

    public List<Integer> findIdsForWord(String txt){
        txt = txt.toLowerCase();
        List<Integer> ret = new ArrayList<Integer>();
        if(prepare(txt)==null) return ret;
        char key = txt.charAt(0);
        TreeMap<String,Word> words = cache.get(key);
        if(words!=null){
            for (Word word : words.values()) {
                if(word.matches(txt)){
                    ret.addAll(word.getIds());
                }
            }
        }
        return ret;
    }

    public void parseText(String text, int id){
        if(text==null || text.length()==0)
            return;

        if(text.contains(" ")){
            String[] strings = text.split(" ");
            for (int i = 0; i < strings.length; i++) {
                String string = strings[i];
                addWord(string, id);
            }
        } else {
            addWord(text, id);
        }
    }

}
